package com.demo.service;

import com.demo.dto.UserDetailDto;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserDetailServiceTmpl implements UserDetailsService {
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("========等待验证的用户名===========");
        System.out.println("username：" + username);
        UserDetailDto userDetailsDto = new UserDetailDto();
        userDetailsDto.setId(1);
        userDetailsDto.setUsername("admin");
        userDetailsDto.setPassword("$2a$10$U6g06YmMfRJXcNfLP28TR.xy21u1A5kIeY/OZMKBDVMbn7PGJiaZS");

        List<String> roles = new ArrayList<>();
        roles.add("ROLE_ADMIN");
        roles.add("ROLE_EMPLOYEE");
        userDetailsDto.setRoles(roles);

        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        for (String role : roles) {
            grantedAuthorities.add(new SimpleGrantedAuthority(role));
        }
        userDetailsDto.setGrantedAuthorities(grantedAuthorities);

        return userDetailsDto;
    }
}
